package br.com.projeto.activity;

import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import br.com.projeto.MegaSenaApplication;
import br.com.projeto.adapter.ListarJogosAdapter;
import br.com.projeto.model.Jogo;
import br.com.projeto.task.ListarJogosTask;
import br.com.projeto.task.SalvarJogosTask;

import com.tbm.megasena.R;

public class SavarJogoActivity extends BaseActivity {

	private ListView lvGames;
	
	private EditText edNumber1;
	private EditText edNumber2;
	private EditText edNumber3;
	private EditText edNumber4;
	private EditText edNumber5;
	private EditText edNumber6;
	private TextView txtId;
	
	private Handler handler;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.salvar_list);

		handler = new Handler(this);

		edNumber1 = (EditText) findViewById(R.id.num1);
		edNumber2 = (EditText) findViewById(R.id.num2);
		edNumber3 = (EditText) findViewById(R.id.num3);
		edNumber4 = (EditText) findViewById(R.id.num4);
		edNumber5 = (EditText) findViewById(R.id.num5);
		edNumber6 = (EditText) findViewById(R.id.num6);
		txtId = (TextView) findViewById(R.id.id);
		lvGames = (ListView) findViewById(R.id.list);
		
		Button btnSend = (Button) findViewById(R.id.enviar);
		btnSend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showProgressDialog(getString(R.string.saving));
				new SalvarJogosTask(handler).execute(atribuirCampos());
			}
		});
		
		this.loadEditTextFocus(edNumber1, edNumber2);
		this.loadEditTextFocus(edNumber2, edNumber3);
		this.loadEditTextFocus(edNumber3, edNumber4);
		this.loadEditTextFocus(edNumber4, edNumber5);
		this.loadEditTextFocus(edNumber5, edNumber6);
	}

	private void loadEditTextFocus(final EditText textView, final EditText textViewNext) {
		textView.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (textView.length() == 2) {
					textViewNext.requestFocus();
					return true;
				}
				return false;
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		showProgressDialog(getString(R.string.loading));
		new ListarJogosTask(handler).execute();
	}
	
	private Jogo atribuirCampos() {
		Jogo jogo = new Jogo();
		jogo.setNumero1(edNumber1.getText().toString());
		jogo.setNumero2(edNumber2.getText().toString());
		jogo.setNumero3(edNumber3.getText().toString());
		jogo.setNumero4(edNumber4.getText().toString());
		jogo.setNumero5(edNumber5.getText().toString());
		jogo.setNumero6(edNumber6.getText().toString());
		jogo.setId(Long.parseLong(txtId.getText().toString()));
		return jogo;
	}
	
	private void cleanValues() {
		edNumber1.setText("");
		edNumber2.setText("");
		edNumber3.setText("");
		edNumber4.setText("");
		edNumber5.setText("");
		edNumber6.setText("");
	}
	
	private void loadValues(Jogo jogo) {
		edNumber1.setText(jogo.getNumero1());
		edNumber2.setText(jogo.getNumero2());
		edNumber3.setText(jogo.getNumero3());
		edNumber4.setText(jogo.getNumero4());
		edNumber5.setText(jogo.getNumero5());
		edNumber6.setText(jogo.getNumero6());
		txtId.setText(jogo.getId().toString());
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean handleMessage(Message msg) {
		super.handleMessage(msg);
		
		switch (msg.what) {
		case MegaSenaApplication.HANDLE_SAVE_OK:
			Toast.makeText(getApplicationContext(), getText(R.string.lbl_jogoOk), Toast.LENGTH_LONG).show();
			this.cleanValues();
			onResume();
			break;
			
		case MegaSenaApplication.HANDLE_SAVE_ERRO:
			Toast.makeText(getApplicationContext(), getText(R.string.lbl_jogoError), Toast.LENGTH_LONG).show();
			break;
			
		case MegaSenaApplication.HANDLE_SAVE_VALIDATE:
			Toast.makeText(getApplicationContext(), getText(R.string.lbl_jogoValidade), Toast.LENGTH_LONG).show();
			break;
		
		case MegaSenaApplication.HANDLE_LIST_OK:
			lvGames.setAdapter(new ListarJogosAdapter(this, (List<Jogo>) msg.obj, handler));
			break;
			
		case MegaSenaApplication.HANDLE_DELETE_OK:
			Toast.makeText(getApplicationContext(), getText(R.string.lbl_jogo_excluido_ok), Toast.LENGTH_LONG).show();
			onResume();
			break;
			
		case MegaSenaApplication.HANDLE_DELETE_ERRO:
			Toast.makeText(getApplicationContext(), getText(R.string.lbl_jogo_excluido_erro), Toast.LENGTH_LONG).show();
			break;
			
		case MegaSenaApplication.HANDLE_UPDATE_OK:
			this.loadValues((Jogo) msg.obj);
			break;
		}
		return true;
	}
}